﻿using Newtonsoft.Json;
using SDDLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PSDManager.SystemSettings
{
    public class Setting
    {
        [JsonIgnore]
        public API API { get; set; }

        public string ApiServerAddress { get; set; }
        public string TcpServerAddress { get; set; }
        public int TcpServerPort { get; set; }

        public string DownloadServerIP { get; set; }

        public ushort DownloadServerPort { get; set; }

        public string DownloadServerUser { get; set; }

        public string DownloadServerPassword { get; set; }

        public int PDMListenPort { get; set; }
        public int PDMSendPort { get; set; }
        public string PemFile { get; set; }

        public Setting()
        {
        }

        public bool Read()
        {
            bool success = false;

            API = new API();

            try
            {
                string path = System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "PDM");
                string json = System.IO.File.ReadAllText(System.IO.Path.Combine(path, "pdmsetting.json"));
                var setting = JsonConvert.DeserializeObject<Setting>(json);

                ApiServerAddress = setting.ApiServerAddress;
                TcpServerAddress = setting.TcpServerAddress;
                TcpServerPort = setting.TcpServerPort;
                PDMListenPort = setting.PDMListenPort;
                PDMSendPort = setting.PDMSendPort;
                DownloadServerIP = setting.DownloadServerIP;
                DownloadServerPort = setting.DownloadServerPort;
                DownloadServerUser = setting.DownloadServerUser;
                DownloadServerPassword = setting.DownloadServerPassword;
                PemFile = setting.PemFile;

                foreach (var p in API.GetType().GetProperties())
                {
                    p.SetValue(API, ApiServerAddress + "api/VideoDownload/" + p.Name);
                }

                success = true;
            }
            catch (Exception ex)
            {
                success = false;
            }

            return success;
        }

        public bool Save()
        {
            bool success = false;

            try
            {
                string path = System.IO.Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "PDM");

                string json = JsonConvert.SerializeObject(this, Formatting.Indented);

                if (!System.IO.Directory.Exists(path))
                {
                    System.IO.Directory.CreateDirectory(path);
                }

                System.IO.File.WriteAllText(System.IO.Path.Combine(path, "pdmsetting.json"), json);

                success = true;
            }
            catch (Exception ex)
            {
                success = false;
            }

            return success;
        }
    }
}
